/* halt.c
 *	Simple program to test whether running a user program works.
 *	
 *	Just do a "syscall" that shuts down the OS.
 *
 * 	NOTE: for some reason, user programs with global data structures 
 *	sometimes haven't worked in the Nachos environment.  So be careful
 *	out there!  One option is to allocate data structures as 
 * 	automatics within a procedure, but if you do this, you have to
 *	be careful to allocate a big enough stack to hold the automatics!
 */

#include "syscall.h"

int lock1, lock2;

int forkFunc2 (void) {
	Write( "\n#### F2:B4###\n\0" , 16, ConsoleOutput );
	LockAcquire(lock1);
	Write( "\n#### F2:AF ###\n\0" , 16, ConsoleOutput );
	LockRelease(lock1);

	DestoryLock(lock1);
	DestoryLock(lock2);	
	Exit (0) ;
}

int forkedFunc (void) {
	Write( "\n#### fork1 ###\n\0" , 16, ConsoleOutput );
 	if ( LockAcquire(lock1) == 1 ) {
		Write( "\n#### f1:OK ###\n\0" , 16, ConsoleOutput );
	}else {
		Write( "\n#### f1:NO ###\n\0" , 16, ConsoleOutput );
	}	
	Fork ((void*)forkFunc2) ;	
	Yield() ;
	Write( "\n#### F1:AF-Y ##\n\0" , 17, ConsoleOutput );	
	LockRelease(lock1);	
	Exit (0);
}

int main()
{

	lock1 = CreateLock() ;
	lock2 = CreateLock() ;
    Fork ((void*)forkedFunc) ;
    Exit(45) ;
}

